package com.javasm.mb_store_api_final.giftsandgoods.goodsType.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.mb_store_api_final.giftsandgoods.goods.entity.GgGoods;
import com.javasm.mb_store_api_final.giftsandgoods.goodsType.service.GgGoodsTypeService;
import com.javasm.mb_store_api_final.giftsandgoods.goodsType.dao.GgGoodsTypeDao;
import com.javasm.mb_store_api_final.giftsandgoods.goodsType.entity.GgGoodsType;
import com.javasm.mb_store_api_final.giftsandgoods.goodsType.vo.TypeBrandVo;
import com.javasm.mb_store_api_final.giftsandgoods.goodsType.vo.TypeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 商品管理-商品类型表(GgGoodsType)表服务实现类
 *
 * @author makejava
 * @since 2024-07-30 10:39:15
 */
@Service("ggGoodsTypeService")
public class GgGoodsTypeServiceImpl extends ServiceImpl<GgGoodsTypeDao, GgGoodsType> implements GgGoodsTypeService {

    @Resource
    GgGoodsTypeDao ggGoodsTypeDao;
    @Override
    public Boolean updateTypeNameById(GgGoods goods) {
        LambdaUpdateWrapper<GgGoodsType> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(GgGoodsType::getId,goods.getGtype());
        lambdaUpdateWrapper.set(GgGoodsType::getTypeName, goods.getTypeName());
        boolean update = this.update(lambdaUpdateWrapper);//??this
        return update;
    }

    @Override
    public GgGoodsType getByTypeName(String voTypeName) {
        GgGoodsType ggGoodsType = ggGoodsTypeDao.getByTypeName(voTypeName);
        return ggGoodsType;
    }

    @Override
    public IPage<GgGoodsType> listByTypeName(TypeVo typeVo) {
        LambdaQueryWrapper<GgGoodsType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(typeVo.getTypeName()!=null&&!typeVo.getTypeName().equals("")){
            lambdaQueryWrapper.like(GgGoodsType::getTypeName,typeVo.getTypeName());
        }
        //分页
        IPage<GgGoodsType> page = new Page<>(typeVo.getPageNum(),typeVo.getPageSize());
        page(page,lambdaQueryWrapper);
        return page;
    }

    @Override
    public boolean insertBrandNameById(TypeBrandVo typeBrandVo) {
        System.out.println(typeBrandVo.getId());
        for(int i = 2; i < typeBrandVo.getBrandIds().length; i++){
            if(typeBrandVo.getBrandIds()[i]!=null){
                Integer brandId = typeBrandVo.getBrandIds()[i];
                Integer id = typeBrandVo.getId();
                ggGoodsTypeDao.insertBrandIdByTypeId(id,brandId);
            }
        }

        //Boolean b=ggGoodsTypeDao.insertBrandNameById(ggGoodsType);
        return true;
    }
}

